package com.dingli.until;

import java.sql.*;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class JdbcTools {

    private String url = "jdbc:mysql://localhost:3306/bcos?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8";
    private String username = "root";
    private String password = "root";

    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;

    //  1.加载驱动类
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public JdbcTools() {
    }

    public JdbcTools(String url, String username, String password) {
        this.url = url;
        this.username = username;
        this.password = password;
    }

    //2.创建连接
    public Connection getConnection() {
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    //  3.构建sql
    public ResultSet executeQuery(String sql, Object... item) {
        try {
            ps = getConnection().prepareStatement(sql);
            for (int i = 0; i < item.length; i++) {
                ps.setObject(i + 1, item[i]);
            }
         //4.执行sql，返回结果集
            rs = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    public int executeUpdate(String sql, Object... item) {
        int count = 0;
        try {
            ps = getConnection().prepareStatement(sql);
            for (int i = 0; i < item.length; i++) {
                ps.setObject(i + 1, item[i]);
            }
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        close();
        return count;
    }

    public List<Map<String, Object>> select(String sql, Object... item) {
        List<Map<String, Object>> result = new LinkedList<>();
        List<String> columnList = new LinkedList<>();
        try {
            ResultSet rs = executeQuery(sql, item);
            ResultSetMetaData md = rs.getMetaData();
            for (int i = 0; i < md.getColumnCount(); i++) {
                columnList.add(md.getColumnName(i + 1));
            }
            while (rs.next()) {
                Map<String, Object> map = new LinkedHashMap<>();
                for (String column : columnList) {
                    map.put(column.toLowerCase(), rs.getObject(column));
                }
                result.add(map);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        close();
        return result;
    }

    public int update(String sql, Object... item) {
        return executeUpdate(sql, item);
    }

    public int insert(String sql, Object... item) {
        return executeUpdate(sql, item);
    }

    public int delete(String sql, Object... item) {
        return executeUpdate(sql, item);
    }

    //  5.关闭资源
    public void close() {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        JdbcTools tools = new JdbcTools();
        List<Map<String, Object>> list = tools.select("select * from user");
        list.forEach(System.out::println);

    }
}
